import { createRouter, createWebHistory } from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { isPublic: true }
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    redirect: '/demo/list',
    children: [
      { path: '/welcome/edit', component: () => import('../components/Welcome') },
      { path: '/users/list', component: () => import('../components/User') },
      { path: '/visitor/list', component: () => import('../components/Visitor') },
      { path: '/comment/list', component: () => import('../components/CommentList') },
      { path: '/tag/list', component: () => import('../components/Tag') },
      { path: '/roles/list', component: () => import('../components/Role') },
      { path: '/articles/create', component: () => import('../components/Article/ArticleEdit.vue') },
      { path: '/articles/edit/:id', component: () => import('../components/Article/ArticleEdit.vue'), props: true },
      { path: '/articles/list', component: () => import('../components/Article/ArticleList.vue') },
      { path: '/demo/create', component: () => import('../components/Demo/DemoEdit.vue') },
      { path: '/demo/edit/:id', component: () => import('../components/Demo/DemoEdit.vue'), props: true },
      { path: '/demo/list', component: () => import('../components/Demo/DemoList.vue') },
      { path: '/about/edit', component: () => import('../components/AboutMe') },
      { path: '/resume', component: () => import('../components/Resume') },
    ]
  },

]

const router = createRouter({
  history: createWebHistory('/admin'),
  routes
})
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login');
  }
  next();
})
export default router
